package com.example.yc23.demo.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.yc23.demo.mapper.UserMapper;
import com.example.yc23.demo.pojo.User;
import com.example.yc23.demo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

@Service
public class UserService implements IService<User> {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;//装配一个redistemplate依赖
    @Override
    public BaseMapper<User> getBaseMapper() {
        return userMapper;
    }
    public JsonResult<User> login(User user){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("username",user.getUsername());
        User loginUser=userMapper.selectOne(wrapper);
        JsonResult<User> jsonResult=new JsonResult<>();
        if (loginUser==null || !loginUser.getPassword().equals(user.getPassword())){
            jsonResult.setData(user);
            jsonResult.setResponseCode(403);
            jsonResult.setResponseMsg("用户名或密码错误");
        }else{
            jsonResult.setData(loginUser);
            jsonResult.setResponseCode(200);
            jsonResult.setResponseMsg("登陆成功！！！！！");
            //登陆成功，生成token
            String token="LOGIN_KEY:"+ UUID.randomUUID().toString();
            jsonResult.setToken(token);
            redisTemplate.opsForValue().set(token,loginUser,30, TimeUnit.MINUTES);
        }
        return jsonResult;

    }

    @Override
    public boolean saveBatch(Collection<User> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<User> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<User> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(User entity) {
        if (entity.getId()==null){
            return save(entity);
        }else{
            return updateById(entity);
        }
    }

    @Override
    public User getOne(Wrapper<User> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<User> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<User> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }
    @Override
    public Class<User> getEntityClass() {
        return null;
    }
}
